package me.goldze.mvvmhabit.lks.module;

import android.app.Application;


import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.lks.LksApplication;

/**
 * <p>
 * Created by ww on 2017/8/31.
 */
public class ModuleInit {
    static List<Class<? extends LksApplication>> lksApplications;

    /**
     * @param aClass
     * @hint
     */
    public static void register(Class<? extends LksApplication> aClass) {
        if (null == lksApplications) {
            lksApplications = new ArrayList<>();
        }
        if (!lksApplications.contains(aClass)) {
            lksApplications.add(aClass);
        }
    }

    public static void onCreate(Application application) {
        if (null == lksApplications || lksApplications.size() <= 0)
            return;
        for (Class<? extends LksApplication> lksApplication : lksApplications) {
            try {
               lksApplication.newInstance().onCreate(application);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    public static void onTerminate() {
        if (null == lksApplications || lksApplications.size() <= 0)
            return;
        for (Class<? extends LksApplication> lksApplication : lksApplications) {
            try {
                lksApplication.newInstance().onTerminate();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

}
